//
//  UserFollowAndFans.swift
//  甜言蜜语
//
//  Created by yingli_yanfa_liuxingyu on 16/8/26.
//  Copyright © 2016年 wl. All rights reserved.
//

import UIKit
import SVProgressHUD
class UserFollowAndFans: UIViewController, UITableViewDataSource, UITableViewDelegate,MyFansCellDelegate {
    var UserID: String!
    var type: String!//如果是type ＝＝ 1 关注 type＝＝ 2 粉丝
    @IBOutlet weak var headerView: UIView!
    @IBOutlet weak var fansNum: UILabel!
    @IBOutlet weak var followNum: UILabel!
    @IBOutlet weak var fans: UILabel!
    @IBOutlet weak var follow: UILabel!
    
    @IBOutlet weak var tableView: UITableView!
    
    var id: String?
    var guanzhu: Bool?
    var modelList: myConcernInfo?{
        didSet{
            self.tableView.reloadData()
            self.followNum.text = self.modelList?.guanzhunum
            self.fansNum.text = self.modelList?.fensinum
            
        }
    }
    var msg: regiestInfo?
    var isMe: Bool?
    override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.tableView.tableHeaderView = headerView
       
        self.title = "Ta的好友"
        
        if self.type == "1"{
            self.follow.textColor = NAVIGATIONBAR_RED_COLOR
            self.followNum.textColor = NAVIGATIONBAR_RED_COLOR
            self.fans.textColor = UIColor.blackColor()
            self.fansNum.textColor = UIColor.blackColor()
        }
        if self.type == "2"{
            self.fans.textColor = NAVIGATIONBAR_RED_COLOR
            self.fansNum.textColor = NAVIGATIONBAR_RED_COLOR
            self.follow.textColor = UIColor.blackColor()
            self.followNum.textColor = UIColor.blackColor()
        }
        
        let cellNib = UINib(nibName: "MyFansCell", bundle: nil)
        self.tableView.registerNib(cellNib, forCellReuseIdentifier: "MyFansCell")
        
        if type == "1"{
            self.getFollowInfo()
        }else{
            self.getFansInfo()
        }
    }
    
    override func viewWillAppear(animated: Bool) {
        self.tabBarController?.tabBar.hidden = true
    }
    //MARK:tableviewDataSource
    
    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        if(cell.respondsToSelector(Selector("setSeparatorInset:"))) {
            cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0)
        }
        if(cell.respondsToSelector(Selector("setLayoutMargins:"))) {
            cell.layoutMargins = UIEdgeInsetsMake(0, 0, 0, 0)
        }
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if modelList?.users.count == nil{
            return 0
        }else{
            return (modelList?.users.count)!
        }
    }
    
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        
        return 55
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCellWithIdentifier("MyFansCell", forIndexPath: indexPath) as! MyFansCell
        
        let model = modelList?.users[indexPath.row]
        cell.model = model
        
        if model?.isme == true{
            cell.AddBtn.setTitle("设置", forState: UIControlState.Normal)
            cell.AddBtn.setImage(nil, forState: UIControlState.Normal)
            
        } else {
            cell.AddBtn.setTitle(nil, forState: UIControlState.Normal)
            
            if model?.isguanzhu == true{
                cell.AddBtn.setImage(UIImage(named: "guanzhu_selected"), forState: UIControlState.Normal)
            }else{
                cell.AddBtn.setImage(UIImage(named: "guanzhu"), forState: UIControlState.Normal)
            }
        }
        
        cell.idLbl.text = model?.name
    
        cell.iv.sd_setImageWithURL(NSURL(string: model!.timg), placeholderImage: UIImage(named: "placeholder"))
        cell.signLbl.text = model?.signature
        cell.delegate = self
        return cell
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        
        let sb = UIStoryboard.init(name: "NewsList", bundle: nil)
        
        let vc = sb.instantiateViewControllerWithIdentifier("UserHomeTableViewController") as! UserHomeTableViewController
        
        vc.userId = Int((self.modelList?.users[indexPath.row].id)!)
        
        self.navigationController?.pushViewController(vc, animated: true)
        
    }

    //MARK:target
    @IBAction func followBtnClicked(sender: UIButton){
        
        self.follow.textColor = NAVIGATIONBAR_RED_COLOR
        self.followNum.textColor = NAVIGATIONBAR_RED_COLOR
        self.fans.textColor = UIColor.blackColor()
        self.fansNum.textColor = UIColor.blackColor()
        self.type = "1"
        
        self.getFollowInfo()
    }
    @IBAction func fansBtnClicked(sender: UIButton){
        
        self.fans.textColor = NAVIGATIONBAR_RED_COLOR
        self.fansNum.textColor = NAVIGATIONBAR_RED_COLOR
        self.follow.textColor = UIColor.blackColor()
        self.followNum.textColor = UIColor.blackColor()
        self.type = "2"
        
        self.getFansInfo()
    }
    // MARK:networking
    //查询一个人的关注
    func getFollowInfo(){
        DataTool.userFollowInfo(UserID!) { (modelList) -> Void in
            guard modelList != nil else{
                return
            }
            
            self.modelList = modelList
        }
    }
    //查询一个人的粉丝
    func getFansInfo(){
        DataTool.userFansInfo(UserID!){ (modelList) -> Void in
            guard modelList != nil else{
                return
            }
            
            self.modelList = modelList
        }
    }
    //MARK:MyFansCellDelegate
    func addFocus(id: String, guanzhu: Bool, isme: Bool){
        if !ApplicationDelegate.isLogin() {
             SVProgressHUD.showInfoWithStatus("请先登录")
        } else if isme == true {
            if !publicMethod.isComplete(){
                let sb = UIStoryboard.init(name: "Login", bundle: nil)
                let vc = sb.instantiateViewControllerWithIdentifier("ChooseTypeTableViewController") as! ChooseTypeTableViewController
                
                self.navigationController?.pushViewController(vc, animated: true)
            }else{
                let type = (NSUserDefaults.standardUserDefaults().objectForKey("k_UD_type")) as! String
                
                if type == "个人"{
                    let sb = UIStoryboard.init(name: "MySelf", bundle: nil)
                    let vc = sb.instantiateViewControllerWithIdentifier("UserInfoTC") as! UserInfoTC
                    self.hidesBottomBarWhenPushed = true
                    self.navigationController?.pushViewController(vc, animated: true)
                    self.hidesBottomBarWhenPushed = false
                }
                if type == "企业"{
                    let sb = UIStoryboard.init(name: "MySelf", bundle: nil)
                    let vc = sb.instantiateViewControllerWithIdentifier("UserCompnayVC") as! UserCompnayVC
                    vc.type = "企业"
                    self.hidesBottomBarWhenPushed = true
                    self.navigationController?.pushViewController(vc, animated: true)
                    self.hidesBottomBarWhenPushed = false
                }
                if type == "美容院"{
                    let sb = UIStoryboard.init(name: "MySelf", bundle: nil)
                    let vc = sb.instantiateViewControllerWithIdentifier("UserBeautyVC") as! UserBeautyVC
                    vc.type = "美容院"
                    self.hidesBottomBarWhenPushed = true
                    self.navigationController?.pushViewController(vc, animated: true)
                    self.hidesBottomBarWhenPushed = false
                }
                
            }

        } else {
            if guanzhu == true{
                DataTool.followOrCancel(id, isguanzhu: false, completionHandler: { (newModel) in
                    guard newModel != nil else {
                        return
                    }
                    self.msg = newModel
                    if self.msg?.msg == "取消关注成功！"{
                        if self.type == "1" {
                            self.getFollowInfo()
                        } else if self.type == "2" {
                            self.getFansInfo()
                        }
                    
                        SVProgressHUD.showSuccessWithStatus(self.msg?.msg)
                        
                    }else{
                        SVProgressHUD.showErrorWithStatus(self.msg?.msg)
                        
                    }
                    
                    
                })
                
            } else {
                DataTool.followOrCancel(id, isguanzhu: true, completionHandler: { (newModel) in
                    guard newModel != nil else {
                        return
                    }
                    self.msg = newModel
                    if self.msg?.msg == "关注成功！"{
                        if self.type == "1" {
                            self.getFollowInfo()
                        } else if self.type == "2" {
                            self.getFansInfo()
                        }
                        SVProgressHUD.showSuccessWithStatus(self.msg?.msg)
                        
                    }else{
                        SVProgressHUD.showErrorWithStatus(self.msg?.msg)
                        
                    }
                    
                    
                })
            }

            
        }
    }
}


        
    

    

